﻿using System;
using System.Windows.Forms;
using LL.Utility.WindowsForms;
using NanoERP.Business.Permission;
using NanoERP.Infrastructure;
using NanoERP.Model;
using NanoERP.Model.Poco;
using NanoERP.Model.SystemMgmt;

namespace NanoERP.Modules.SystemMgmt
{
    [Module("FrmMgrDic")]
    public partial class FrmMgrDic : ModuleForm
    {
        #region 属性

        private SelectedType _selectedType;
        private SelectedType SelectedType
        {
            get { return _selectedType; }
            set
            {
                _selectedType = value;
                ChangeUiState();
            }
        }

        #endregion

        #region 构造

        public FrmMgrDic()
        {
            InitializeComponent();
            InitControls();
            if (DesignMode)
                return;
            LoadData();
        }

        #endregion

        #region 私有方法

        private void LoadData()
        {
            //this.treeView1.Nodes[0].Nodes.Clear();
            //DataDictionaryTree root = null;
            //using (var session = BusinessSessionFactory.NewSession())
            //{
            //    root = session.Get<DataDictionaryService>().GetDicTree();
            //}
            //BuildNode(this.treeView1.Nodes[0].Nodes, root);
            //if (this.treeView1.Nodes[0] != null)
            //    this.treeView1.Nodes[0].ExpandAll();
        }

        private void ReLoadData()
        {
            var path = treeView1.SelectedNode.FullPath;
            LoadData();
            var node = TreeViewHelper.FindNodeOnPath(treeView1.Nodes, path);
            treeView1.SelectedNode = node;
        }

        private void InitControls()
        {
            InitTreeView();
            ChangeUiState();
        }

        private void InitTreeView()
        {

            var tvHelper = new TreeViewHelper(this.treeView1);
            tvHelper.HandleDragDrop();
            tvHelper.DropCompleted += tvHelper_DropCompleted;
            tvHelper.EnableCheckAll();
        }

        private void ChangeUiState()
        {
            foreach (ToolStripItem item in toolStrip1.Items)
            {
                item.Enabled = false;
            }
            switch (this.SelectedType)
            {
                case SelectedType.DataDictionaryType:
                    tsAddType.Enabled = true;
                    tsAddDic.Enabled = true;
                    tsEdit.Enabled = true;
                    tsDel.Enabled = true;
                    break;
                case SelectedType.DataDictionary:
                    tsEdit.Enabled = true;
                    tsDel.Enabled = true;
                    break;
                default:
                    tsAddType.Enabled = true;
                    tsAddDic.Enabled = true;
                    break;
            }
        }

        #endregion

        #region 静态方法

        private static void BuildNode(TreeNodeCollection nodes, DataDictionaryTree root)
        {
            foreach (var child in root.Children)
            {
                var node = new TreeNode(child.Name)
                {
                    Tag = child,
                };
                BuildNode(node.Nodes, child);
                nodes.Add(node);
            }
            foreach (var role in root.Items)
            {
                var node = new TreeNode(role.Name)
                {
                    Tag = role,
                    ImageKey = "role.png",
                    SelectedImageKey = "role.png",
                };
                nodes.Add(node);
            }
        }

        #endregion

        #region 事件处理

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node == treeView1.Nodes[0])
            {
                SelectedType = SelectedType.None;
                return;
            }
            if (e.Node.Tag as SYS_DataDictionaryType != null)
            {
                SelectedType = SelectedType.DataDictionaryType;
                var frm = new FrmEditDicType()
                {
                    TopLevel = false,
                    Data = e.Node.Tag as SYS_DataDictionaryType,
                    Dock = DockStyle.Fill,
                    FormBorderStyle = FormBorderStyle.None
                };
                frm.Enabled = false;
                panel1.Controls.Clear();
                panel1.Controls.Add(frm);
                frm.Show();
                return;
            }
            if (e.Node.Tag as SYS_DataDictionary != null)
            {
                SelectedType = SelectedType.DataDictionary;
                var frm = new FrmEditDic()
                {
                    TopLevel = false,
                    Data = e.Node.Tag as SYS_DataDictionary,
                    Dock = DockStyle.Fill,
                    FormBorderStyle = FormBorderStyle.None
                };
                frm.Enabled = false;
                panel1.Controls.Clear();
                panel1.Controls.Add(frm);
                frm.Show();
                return;
            }
        }

        private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            //if (e.CancelEdit || e.Label == null)
            //    return;
            //if (e.Node.Tag is SYS_DataDictionaryType)
            //{
            //    var dep = e.Node.Tag as SYS_DataDictionaryType;
            //    dep.Name = e.Label.Trim();

            //    new DataDictionaryService().UpdateDicType(dep);
            //}
            //if (e.Node.Tag is SYS_DataDictionaryType)
            //{

            //}
        }

        private void tvHelper_DropCompleted(object sender, DropCompleateArgs e)
        {

            TreeNode destinationNode = e.DestinationNode;
            TreeNode sourceNode = e.SourceNode;
            if (destinationNode.Tag is SYS_DataDictionary
                || !MessageDialogHelper.ConfirmYesNo("确认移动？"))
            {
                e.Cancel = true;
                return;
            }
            var dep = destinationNode.Tag as SYS_DataDictionaryType;
            var dep2 = sourceNode.Tag as SYS_DataDictionaryType;
            var user = sourceNode.Tag as SYS_DataDictionary;
            Guid? desId = dep == null ? null : new Guid?(dep.Id);
            if (dep2 != null)
            {
                using (var depBo = new DataDictionaryService())
                    depBo.MoveType(dep2.Id, desId);
                return;
            }
            new DataDictionaryService().Move(user.Id, desId.Value);
        }

        #endregion

        #region 工具栏操作

        private void tsAddType_Click(object sender, EventArgs e)
        {
            var dep = treeView1.SelectedNode.Tag as SYS_DataDictionaryType;
            if (dep == null)
                return;
            var frm = new FrmEditDicType()
            {
                ParentId = dep.Id
            };
            if (frm.ShowDialog(this) == DialogResult.OK)
                ReLoadData();
        }

        private void tsAddDic_Click(object sender, EventArgs e)
        {
            var dep = treeView1.SelectedNode.Tag as SYS_DataDictionaryType;
            if (dep == null)
                return;
            var frm = new FrmEditDic()
            {
                TypeId = dep.Id
            };
            if (frm.ShowDialog(this) == DialogResult.OK)
                ReLoadData();
        }

        private void tsEdit_Click(object sender, EventArgs e)
        {
            if (SelectedType == SelectedType.DataDictionaryType)
            {
                var frm = new FrmEditDicType()
                {
                    Data = treeView1.SelectedNode.Tag as SYS_DataDictionaryType
                };
                if (frm.ShowDialog(this) == DialogResult.OK)
                    ReLoadData();
            }
            else
            {
                var frm = new FrmEditDic()
                {
                    Data = treeView1.SelectedNode.Tag as SYS_DataDictionary
                };
                if (frm.ShowDialog(this) == DialogResult.OK)
                    ReLoadData();
            }
        }


        private void tsDel_Click(object sender, EventArgs e)
        {
            if (!MessageDialogHelper.ConfirmYesNo("确认删除？"))
                return;
            var entity = treeView1.SelectedNode.Tag as SYS_DataDictionary;
            if (entity == null)
                return;
            if (SelectedType == SelectedType.DataDictionary)
            {
                new DataDictionaryService().DeleteDic(entity.Id);
            }
            else
            {
                new DataDictionaryService().DeleteDicType(entity.Id);
            }
            ReLoadData();
            MessageDialogHelper.ShowInfomation("删除成功");
        }

        #endregion
    }
}
